<?php
	//全局配置文件

	$_index_ = __FILE__;

	if ( ! isset( $GLOBALS[$_index_] ) )
	{
		$GLOBALS[$_index_] = array();
		$GLOBALS[$_index_]['database'] = array();
		$GLOBALS[$_index_]['tpl'] = array();
		$GLOBALS[$_index_]['domain'] = array();
		$GLOBALS[$_index_]['dir'] = array();
		$GLOBALS[$_index_]['cache'] = array();

		//database config
		//write
		$GLOBALS[$_index_]['database']['write']['host'] = 'localhost';
		$GLOBALS[$_index_]['database']['write']['name'] = 'pcms';
		$GLOBALS[$_index_]['database']['write']['user'] = 'pcms';
		$GLOBALS[$_index_]['database']['write']['pass'] = 'pcms';
		//read
		$GLOBALS[$_index_]['database']['read']['host'] = 'localhost';
		$GLOBALS[$_index_]['database']['read']['name'] = 'pcms';
		$GLOBALS[$_index_]['database']['read']['user'] = 'pcms';
		$GLOBALS[$_index_]['database']['read']['pass'] = 'pcms';

		//domain config
		$host_name = $_SERVER['HTTP_HOST'];
		$host_arg = explode( '.', $host_name );
		//$host_header = $host_arg[0];
		$GLOBALS[$_index_]['domain']['root'] = '.' . $host_arg[count($host_arg)-2] . '.' . $host_arg[count($host_arg)-1];

		//后台域名
		$GLOBALS[$_index_]['domain']['admin'] = 'http://' . $host_name . '/admin/';
		$GLOBALS[$_index_]['domain']['tpl']['admin'] = $GLOBALS[$_index_]['domain']['admin'] . 'tpl/';
		//前台域名
		$GLOBALS[$_index_]['domain']['www'] = 'http://' . $host_name . '/www/';
		$GLOBALS[$_index_]['domain']['tpl']['root'] = 'http://' . $host_name . '/tpl/';
		$GLOBALS[$_index_]['domain']['tpl']['www'] = $GLOBALS[$_index_]['domain']['www'] . 'tpl/';
		$GLOBALS[$_index_]['domain']['widget'] = 'http://' . $host_name . '/widget/';

		//path info
		$data['root'] = $_SERVER['DOCUMENT_ROOT'];
		if ( substr( $data['root'], -1 ) == '/' || substr( $data['root'], -1 ) == '\\' )
		{
			$data['root'] = substr( $data['root'], 0, ( strlen( $data['root'] ) - 1 ) );
		}
		$data['directory'] = dirname( $_SERVER['SCRIPT_NAME'] ) . '/';

		//base config
		$GLOBALS[$_index_]['library_dir'] = $data['root'] . '/lib/';
		$GLOBALS[$_index_]['biz_dir'] = $data['root'] . '/biz/';
		$GLOBALS[$_index_]['directory'] = $data['directory'];
		$GLOBALS[$_index_]['server'] = & $_SERVER;
		$GLOBALS[$_index_]['cookie'] = & $_COOKIE;

		$GLOBALS[$_index_]['host'] = 'http://' . $GLOBALS[$_index_]['server']['HTTP_HOST'];
		$GLOBALS[$_index_]['current_url'] = $GLOBALS[$_index_]['host'] . $GLOBALS[$_index_]['server']['REQUEST_URI'];
		$GLOBALS[$_index_]['current_directory_url'] = $GLOBALS[$_index_]['host'] . $GLOBALS[$_index_]['directory'];

		//dir config
		$GLOBALS[$_index_]['dir']['root'] = $data['root'] . '/';
		$GLOBALS[$_index_]['dir']['www'] = $data['root'] . '/www/';
		$GLOBALS[$_index_]['dir']['admin'] = $data['root'] . '/admin/';

		$GLOBALS[$_index_]['dir']['tpl']['root'] = $GLOBALS[$_index_]['dir']['www'] . '/tpl/';
		$GLOBALS[$_index_]['dir']['tpl']['admin'] = $GLOBALS[$_index_]['dir']['admin'] . 'tpl/';
		$GLOBALS[$_index_]['dir']['tpl']['www'] = $GLOBALS[$_index_]['dir']['www'] . 'tpl/';

		$GLOBALS[$_index_]['dir']['upload'] = $GLOBALS[$_index_]['dir']['tpl']['www'] . 'tpl/';
		$GLOBALS[$_index_]['dir']['virtual_upload'] = 'upload/' . date( 'Y-m' ) . '/';
		$GLOBALS[$_index_]['dir']['physical_upload'] = $GLOBALS[$_index_]['dir']['upload'] . $GLOBALS[$_index_]['dir']['virtual_upload'];
		$GLOBALS[$_index_]['dir']['taglib'] = $GLOBALS[$_index_]['biz_dir'] . 'taglib/';

		//cache config
		$GLOBALS[$_index_]['cache']['base'] = $data['root'] . '/data';
		$GLOBALS[$_index_]['cache']['data'] = $GLOBALS[$_index_]['cache']['base'] . 'cache/';


		/**
		 * 获取配置参数
		 * 参数如domain.www
		 * @param string $name 逗号分隔下标索引
		 * @return mixed
		 */
		function config( $name = '' )
		{
			$_index_ = __FILE__;

			if ( $name == '' )
			{
				$result = $GLOBALS[$_index_];
			}
			else
			{
				$temp = explode( '.', $name );
				$size = count( $temp );

				if ( $size == 1 )
				{
					$result = isset( $GLOBALS[$_index_][$name] ) ? $GLOBALS[$_index_][$name] : '';
				}
				else if ( $size == 2 )
				{
					$result = isset( $GLOBALS[$_index_][$temp[0]][$temp[1]] ) ? $GLOBALS[$_index_][$temp[0]][$temp[1]] : '';
				}
				else if ( $size == 3 )
				{
					$result = isset( $GLOBALS[$_index_][$temp[0]][$temp[1]][$temp[2]] ) ? $GLOBALS[$_index_][$temp[0]][$temp[1]][$temp[2]] : '';
				}
				else if ( $size == 4 )
				{
					$result = isset( $GLOBALS[$_index_][$temp[0]][$temp[1]][$temp[2]][$temp[3]] ) ? $GLOBALS[$_index_][$temp[0]][$temp[1]][$temp[2]][$temp[3]] : '';
				}
				else
				{
					trigger_error( '[config function] wrong argument.', E_USER_ERROR );
				}
			}

			return $result;
		}


		/**
		 * 加截类库
		 * detail description
		 * @param string $class
		 * @param array $config
		 * @param boolean $unique
		 * @return object
		 */
		function load( $class, $config = array(), $unique = true )
		{
			static $object = array();

			if ( ! isset( $object[$class] ) || ! $unique )
			{
				if ( preg_match( '/^biz\..*/', $class ) )
				{
					$class = preg_replace( '/^biz\./', '', $class );
					$require_root = config( 'biz_dir' );
				}
				else
				{
					$require_root = config( 'library_dir' );
				}

				$rfile = $require_root . str_replace( '.', '/', $class ) . '.php';
				if ( is_file( $rfile ) && is_readable( $rfile ) )
				{
					require_once $require_root . str_replace( '.', '/', $class ) . '.php';

					$name = explode( '.', $class );
					$size = count( $name );
					$name = $size > 0 ? $name[$size-1] : $name;
				}
				else
				{
					die( 'load class error : ' . $rfile );
				}

				if ( $name == 'template' )
				{
					$style = isset( $config['style'] ) ? $config['style'] : 1;
					$template_dir_path = isset( $config['domain'] ) ? config( 'dir.' . $config['domain'] ) : config( 'dir.tpl' );
					$template_url_path = isset( $config['domain'] ) ? config( 'domain.' . $config['domain'] ) : config( 'domain.tpl' );

					$temp = new $name( $template_dir_path . $style . '/' );

					$template_config = config();
					$template_config['tpl']['images'] = $template_url_path . $style . '/' . 'images/';
					$template_config['tpl']['js'] = $template_url_path . $style . '/' . 'js/';
					$template_config['tpl']['css'] = $template_url_path . $style . '/' . 'css/';
					$template_config['tpl']['url.root'] = $template_url_path . $style . '/';
					$template_config['tpl']['dir.root'] = $template_dir_path . $style . '/';
					$temp->auto = array( '_tpl' => $template_config['tpl'], '_domain' => $template_config['domain'] );
				}
				else if ( $name == 'database' )
				{
					$database_config = config( 'database' );

					$temp = new $name( $database_config );
				}
				else if ( $name == 'cache' )
				{
					$temp = new $name( config( 'cache.data' ) );
				}
				else if ( $name == "cookie" )
				{
					$temp = new $name( config( 'cookie' ), '', config( 'domain.root' ) );
				}
				else
				{
					$size = count( $config );
					$count = 1;
					foreach( $config as $value ) ${'arg_'.$count++} = $value;

					if ( $size == 0 )
					{
						$temp = new $name();
					}
					else if ( $size == 1 )
					{
						$temp = new $name( $arg_1 );
					}
					else if ( $size == 2 )
					{
						$temp = new $name( $arg_1, $arg_2 );
					}
					else if ( $size == 3 )
					{
						$temp = new $name( $arg_1, $arg_2, $arg_3 );
					}
					else if ( $size == 4 )
					{
						$temp = new $name( $arg_1, $arg_2, $arg_3, $arg_4 );
					}
					else if ( $size == 5 )
					{
						$temp = new $name( $arg_1, $arg_2, $arg_3, $arg_4, $arg_5 );
					}
					else if ( $size == 6 )
					{
						$temp = new $name( $arg_1, $arg_2, $arg_3, $arg_4, $arg_5, $arg_6 );
					}
					else
					{
						trigger_error( 'load function miss ' . $class . ' construct argument.', E_USER_ERROR );
					}
				}

				if ( $name != 'template' ) $object[$class] = & $temp;
			}
			else
			{
				$temp = $object[$class];
			}

			if ( ! is_object( $temp ) )
			{
				trigger_error( 'can not load' . $class . ' object.', E_USER_ERROR );
			}

			return $temp;
		}
	}
?>